/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.stock;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */
@ManagedBean(name="createStock")
@SessionScoped

public class CreateStock implements Serializable {
    
     @Resource(name="jdbc/antonsDatasource")
    
     private DataSource ds;
     int stat;
    private int itemID;
    private String itemName;
    private String itemDescription;
    private String itemRarity;
    private int itemStock;
    private int rarity =0;
    
    private String msg="";

    
    
    /*-------------------------------------------------------------------------*/
    // getter and setters
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    
    

    public int getItemID() {
        return itemID;
    }

    public void setItemID(int itemID) {
        this.itemID = itemID;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getItemRarity() {
        return itemRarity;
    }

    public void setItemRarity(String itemRarity) {
        this.itemRarity = itemRarity;
    }

    public int getItemStock() {
        return itemStock;
    }

    public void setItemStock(int itemStock) {
        this.itemStock = itemStock;
    }
    
  /*----------------------------------------------------------------------------*/
 
   
   // method used to insert a stock item in database  
    public  String add( ) throws SQLException {
       
       
      
       if(ds==null)
			throw new SQLException("Can't get data source");// throws exception i database not available
 
		//database connection
		Connection con = ds.getConnection();
                
                
            //insert into Stock (item_id, item_name, item_description, item_rarity, item_stock) values (?,?,?,?,?);

		if(con==null)
			throw new SQLException("Can't get database connection");
         try (PreparedStatement ps = con.prepareStatement(
                 "insert into Stock (item_id, item_name, item_description, item_rarity, item_stock) values (?,?,?,?,?);" )){
             
             ps.setInt( 1,  itemID); // sets each "?" to recieved value
             ps.setString(2, itemName);
             ps.setString(3,itemDescription);
             ps.setInt(4, Integer.parseInt(itemRarity));
             ps.setInt(5, itemStock);
             
            stat = ps.executeUpdate();
            
             System.out.println("Data Added Successfully");

             
         }
         reset();
         con.close();
         return "/admin/stock/createstock?faces-redirect=true";// tells where to go afte action is performed 
        
    }  
    
    public void reset()
    {
     this.itemID=0;
     this.itemDescription=null;
     this.itemName= null;
     this.itemRarity="0";
     this.itemStock=0;
    
    }

    /**
     * @return the stockEntity
     */
   
    
    
    
    
}
